package com.ycsj.goldmedalnewconcept.common.util;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.TeacherInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请求校长名下所有老师的工具类
 * Created by Administrator on 2019/4/16 0016.
 */

public class TeacherUtils {

    /**
     * 获取班级列表的请求接口
     *
     * @param rlLoading 加载视图
     */
    public static void httTeacher(final View rlLoading, Object object, final onSuccessListener listener) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "15");
        map.put("c", AccountUtils.getAccount(BaseApplication.context));
        map.put("role", AccountUtils.getRole(BaseApplication.context));

        HttpUtils.post(HttpAddress.GETTEACHER, map, object, new HttpCallBack<TeacherInfo>() {
            @Override
            public void onSuccess(TeacherInfo info) {
                if (null != info) {
                    if ("1".equals(info.state)) {
                        //返回成功
                        List<TeacherInfo.ListBean> teacherInfos = info.list;

                        if (null != teacherInfos && teacherInfos.size() != 0) {
                            //使用回调接口将获取到的班级列表的数据回调出去
                            if (listener != null) {
                                listener.onSuccess(teacherInfos);
                            }

                        } else {
                            if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                                rlLoading.setVisibility(View.GONE);
                            }
                            ToastUtils.showShort("没有班级数据");
                            if (listener != null) {
                                listener.hasNoData();
                            }
                        }

                    } else {
                        //返回错误
                        ToastUtils.showShort("服务器返回异常,状态码是:" + info.state);
                        Log.i("test", "服务器返回异常,状态码是:" + info.state);
                    }

                } else {
                    Log.i("test", "服务器返回数据为空");
                }
            }

            @Override
            public void onError(String e) {
                if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 回调接口
     * 成功获取到老师数据后将数据回调出去
     */
    public interface onSuccessListener {
        void onSuccess(List<TeacherInfo.ListBean> datas);

        void hasNoData();
    }
}
